<!-- 正文开始 -->
<style>
    .companyFormPhotos {
        /* margin-left:140px; */
        line-height: 38px;
        float: left;
        margin-right: 5px;
    }

    .companyFormPhotos img {
        cursor: pointer;
        height: 38px;
        width: auto;
        max-width: 100px;
    }

    .layui-table-cell .layui-table-sort {
        display: none;
    }
    .inspectClaimForm .layui-form-label{
        width: 85px;
    }
    .inspectClaimForm .layui-input-block{
        margin-left: 115px;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <!-- 设备类型 -->
                                <div class="layui-inline">
                                    <label class="layui-form-label">设备类型：</label>
                                    <div class="layui-input-inline">
                                        <select id='deviceType' name="deviceType"
                                                lay-filter="deviceType"
                                                class="common-select-search">
                                        </select>
                                    </div>
                                </div>
                                <!-- 设备状态 -->
                                <div class="layui-inline">
                                    <label class="layui-form-label">设备状态：</label>
                                    <div class="layui-input-inline">
                                        <select id='inspectClaimStatus' name="inspectClaimStatus"
                                                lay-filter="inspectClaimStatus"
                                                class="common-select-search">
                                            <option value="">请选择状态</option>
                                            <option value="1">待认领</option>
                                            <option value="2">已认领</option>
                                            <option value="3">完成</option>
                                        </select>
                                    </div>
                                </div>
                                <!-- 设备编码 sku -->
                                <div class="layui-inline">
                                    <label class="layui-form-label">设备编码：</label>
                                    <div class="layui-input-inline">
                                        <input id="deviceId" name="deviceId" class="layui-input" type="text" />
                                    </div>
                                </div>
                                <!-- 设备编码 sku -->
                                <div class="layui-inline">
                                    <label class="layui-form-label">任务编码：</label>
                                    <div class="layui-input-inline">
                                        <input id="id" name="id" class="layui-input" type="text" />
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green"
                                            lay-filter="formSubSearchLog_main"
                                            lay-submit layui-form-keyDownSearch="true">查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>
                                        重置
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="inspectClaimTableInfo" lay-filter="inspectClaimTableInfo"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="inspectClaimTableBar">
    {{# if(d.examineType !='1') {  }}
    <a class="layui-btn layui-btn-warm  layui-btn-xs" lay-event="claim">认领登记</a>
    {{# } }}

</script>


<!-- 表单弹窗修改/添加 -->
<script type="text/html" id="inspectClaimForm">
    <table class="layui-table" id="inspectClaimInfo" lay-filter="inspectClaimInfo"></table>
    <form lay-filter="inspectClaimForm" class="layui-form inspectClaimForm model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item layui-row">
            <div class="layui-col-md3">
                <label class="layui-form-label">检验机构：</label>
                <input id="supplierName" type="hidden" name="supplierName"/>
                <div class="layui-input-block">
                    <select lay-search="" id="supplierId" name="supplierId" lay-filter="supplierId"
                            class="common-select-search">
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-row">
            <div class="layui-col-md3">
                <label class="layui-form-label">检验员：</label>
                <div class="layui-input-block">
                    <input id="supplierContactsName" name="supplierContactsName" class="layui-input" type="hidden" />
                    <select lay-search="" id="supplierContactsId" name="supplierContactsId" lay-filter="supplierContactsId"
                            class="common-select-search">
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-row">
            <div class="layui-col-md3">
                <label class="layui-form-label">检验结果：</label>
                <div class="layui-input-block">
                    <select lay-search="" id="examineType" name="examineType" lay-filter="examineType"
                            class="common-select-search">
                        <option value="" selected>请选择</option>
                        <option value="1">合格</option>
                        <option value="2">不合格</option>
                        <option value="3">整改</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-row">
            <div class="layui-col-md3">
                <label class="layui-form-label">检测报告：</label>
                <div class="layui-input-block">
                    <div id="inspectReportPath" class="companyFormPhotos"></div>
                    <div id="btnInspectReport" class="layui-btn" style="float: left;"><i class="layui-icon">&#xe681;</i>上传</div>
                    <input name="inspectReport" type="hidden" class="layui-input" lay-verify="" id="inspectReport"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-row">
            <div class="layui-col-md3">
                <label class="layui-form-label">产生费用：</label>
                <div class="layui-input-block">
                    <input id="inspectCost" name="inspectCost" type="text" class="layui-input" lay-verify="number" required />
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-row">
            <div class="layui-col-md3">
                <label class="layui-form-label">备注说明：</label>
                <div class="layui-input-block">
                    <input id="remark" name="remark" type="text" class="layui-input" />
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="inspectClaimFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'tableTreeDj', 'upload', 'config','laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;
        var parentId ;
        admin.reqSync('enterprise/dict/admin/dictLabel/list?page=1&limit=10&dictType=sys_device_type_code', {}, function (res) {
            if (res.code == 200) {
                parentId = res.data[0].labelValue;
            }
        });
        // 设备类型
        admin.initDataSelect('enterprise/product/admin/productType/list?page=1&limit=9999&parentId='+parentId, {},"deviceType",null,"设备类型",null,null );


        // 渲染表格
        var insTb = table.render({
            elem: '#inspectClaimTableInfo',
            url: config.base + 'enterprise/device/admin/inspectClaim/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {align: 'center', type: 'numbers', title: '序号', fixed: 'left'}
                , {align: 'center', field: 'claimNo', sort: true, title: '年检认领记录编号', fixed: 'left', width: '14%'}
                , {align: 'center', field: 'inspectId',sort: true, title: '年检任务编号', fixed: 'left'}
                , {align: 'center', field: 'deviceId',sort: true, title: '设备编号', fixed: 'left'}
                , {align: 'center', field: 'deviceName',sort: true, title: '设备名称', fixed: 'left'}
                , {align: 'center', field: 'examineTypeDesc', sort: true, title: '年检结果'}
                , {align: 'center', field: 'supplierName', sort: true, title: '检测机构'}
                , {align: 'center', field: 'inspectCost', sort: true, title: '检测费用时间'}
                , {align: 'center', field: 'inspectStatusDesc', sort: true, title: '状态'}
                , {align: 'center', field: 'createTime', sort: true, title: '创建时间'}
                , {align: 'center', field: 'creatorName', sort: true, title: '认领人'}
                , {align: 'center', field: 'creatorName', sort: true, title: '认领时间'}
                , {
                    align: 'center',
                    toolbar: '#inspectClaimTableBar',
                    title: '操作',
                    width: 250,
                    fixed: 'right',
                    width: '14%'
                }
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if (menuButton.indexOf("搜索") == -1) {
                    $('.layui-form.toolbar').remove();
                }
                if (menuButton.indexOf("修改") == -1) {
                    $("[lay-event='edit']").remove();
                }
                if (menuButton.indexOf("删除") == -1) {
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(inspectClaimTableInfo)', function (obj) {
            var data = obj.data;
            if (obj.event === 'claim') { //认领
                showEditModel(data , 1);
            }else if (obj.event === 'detail') { //查看
                showEditModel(data, 2);
            }
        });

        //监听排序
        table.on('sort(inspectClaimTableInfo)', function (obj) {
            table.reload('inspectClaimTableInfo', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog_main)', function (data) {
            data.field.status = data.field.statusSearch_main;
            data.field.postId = data.field.postIdSearchMain;
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', data => {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });


        // 显示编辑弹窗
        function showEditModel(data, type) {
            var tableData = [{...data}];
            admin.open({
                type: 1,
                area: '1300px',
                offset: '65px',
                title: '编辑年检认领记录',
                content: $('#inspectClaimForm').html(),
                success: function () {
                    table.render({
                        elem: '#inspectClaimInfo',
                        data: tableData,
                        page: true,
                        cellMinWidth: 100,
                        cols: [[
                            {align: 'center', type: 'numbers', title: '序号', fixed: 'left'}
                            , {align: 'center', field: 'claimNo', sort: true, title: '年检认领记录编号', fixed: 'left', width: '14%'}
                            , {align: 'center', field: 'inspectId',sort: true, title: '年检任务编号', fixed: 'left'}
                            , {align: 'center', field: 'deviceId',sort: true, title: '设备编号', fixed: 'left'}
                            , {align: 'center', field: 'deviceName',sort: true, title: '设备名称', fixed: 'left'}
                            , {align: 'center', field: 'examineType', sort: true, title: '年检结果'}
                            , {align: 'center', field: 'supplierName', sort: true, title: '检测机构'}
                            , {align: 'center', field: 'inspectCost', sort: true, title: '检测费用时间'}
                            , {align: 'center', field: 'inspectStatus', sort: true, title: '状态'}
                            , {align: 'center', field: 'createTime', sort: true, title: '创建时间'}
                            , {align: 'center', field: 'creatorName', sort: true, title: '认领人'}
                            , {align: 'center', field: 'creatorName', sort: true, title: '认领时间'}
                        ]],
                        done: function (res, curr, count) {
                            //移除按钮
                        }
                    });
                    //查询设备供应商
                    admin.reqSync('enterprise/purchase/admin/supplierInfoPass/getAll', {}, function (res) {
                        if (res.code === 200) {
                            loadSelectHtml(res.data, "请选择设备供应商", "supplierId", '[lay-filter="inspectClaimForm"]', 3);
                        } else {
                            loadSelectHtml([], "请选择设备供应商", "supplierId", '[lay-filter="inspectClaimForm"]', 3);
                        }
                    }, 'get');
                    //选择 供应商
                    form.on('select(supplierId)', function (data) {
                        if (data.value === '') {
                        } else {
                            let dataObj = JSON.parse($(data.elem[data.elem.selectedIndex]).attr('data'));
                            $('form[lay-filter="inspectClaimForm"] input[name="supplierName"]').val(dataObj.corporateName);
                            showSupplierContacts(dataObj);

                        }
                    });
                    $('#btnInspectReport').click(uploadImg('btnInspectReport', 'inspectReportPath', 'inspectReport'));
                    // 查看大图
                    layer.photos({
                        photos: '.companyFormPhotos'
                        ,anim: 5
                    });
                    //检验员 下拉
                    function showSupplierContacts(data) {
                        admin.reqSync('enterprise/purchase/admin/supplierContacts/list', {'corporateId':data.id}, function (res) {
                            if (res.code === 200) {
                                loadSelectHtml(res.data, "请选择检验员", "supplierContactsId", '[lay-filter="inspectClaimForm"]', 1);
                            } else {
                                loadSelectHtml([], "请选择检验员", "supplierContactsId", '[lay-filter="inspectClaimForm"]', 1);
                            }
                        }, 'get');
                    }
                    form.on('select(supplierContactsId)', function (data) {
                        if (data.value === '') {
                        } else {

                            let dataObj = JSON.parse($(data.elem[data.elem.selectedIndex]).attr('data'));
                            $('form[lay-filter="inspectClaimForm"] input[name="supplierContactsName"]').val(dataObj.contactName);
                        }
                    });

                    //设置默认值
                    form.render();
                    // 表单提交事件
                    form.on('submit(inspectClaimFormSubmit)', function (d) {
                        layer.load(2);
                        console.log(d.field);
                        let params = {
                            ...d.field,
                            'inspectId': tableData[0].inspectId,
                            'inspectClaimId': tableData[0].inspectClaimId,
                        }
                        //新增修改
                        admin.req('enterprise/device/admin/inspectClaim/add', JSON.stringify(params), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('inspectClaimTableInfo');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST', true, 'application/json; charset=utf-8');
                        return false;
                    });
                }
            });
            // 上传图片
            function uploadImg(_elem, pathId, inputId) {
                upload.render({
                    elem: '#' + _elem
                    , url: config.upload_url + 'uploadFile'
                    , data: {}
                    , accept: 'file'
                    , exts: 'jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx'
                    , headers: { Authorization: 'Bearer ' + config.getToken() }
                    , done: function (res) {
                        if (res.code == 200) {
                            layer.msg(res.msg || "上传成功", { icon: 1 });
                            let imagUrl = res.data.url;
                            $('#' + pathId).html('<img src="' +imagUrl + '"/>');
                            $('#' + inputId).val(imagUrl);
                        } else if (res.code == 502) {
                            return location.replace('admin/login.html');
                        } else {
                            layer.msg(res.msg || "上传失败", { icon: 2 });
                        }
                    }
                });
            }

            /**
             * 加载地址HTML
             * @param data 下拉菜单数据
             * @param tips select 默认提示
             * @param htmltarget html要插入的位置
             * @param name  下拉框需要显示值的属性
             */
            function loadSelectHtml(data, tips, htmltarget, formName, type) {
                let html = ' <option value>' + tips + '</option>';
                if (!data || data == null) {
                    $("form" + formName + " select[name='" + htmltarget + "']").html(html);
                    form.render('select');
                    return;
                }
                for (let i = 0; i < data.length; i++) {
                    if (type == 1) {
                        html += `<option value='${data[i].id}' data='${JSON.stringify(data[i])}'>${data[i].contactName}</option>`;
                    } else if (type == 2) {
                        html += `<option value='${data[i].labelValue}' data='${JSON.stringify(data[i])}'>${data[i].labelName}</option>`;
                    } else if (type == 3) {
                        html += `<option value='${data[i].id}' data='${JSON.stringify(data[i])}'>${data[i].corporateName}</option>`;
                    }
                }
                $("form" + formName + " select[name='" + htmltarget + "']").html(html);
                form.render('select');
            }
        }
    });
</script>